package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.bo.OutStorageBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.OutStorageMapper;
import com.liuzu.esys.pojo.OutStorage;
import com.liuzu.esys.pojo.Storage;
import com.liuzu.esys.service.OutStorageService;
import com.liuzu.esys.vo.IntoStorageVo;
import com.liuzu.esys.vo.OutStorageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 太上老君保佑：代码，绿绿绿绿绿！！！！！
 *
 * @Description :
 * @Author : 冯小冯
 * @Date : 2022-07-26 19:28
 */
@Service("/outStorageService")
public class OutStorageServiceImpl implements OutStorageService {
    @Autowired
    private OutStorageMapper outStorageMapper;

    @Override
    public LayuiPageVo<OutStorage> outStorageService(Map<String, Object> params) {
        // mybatis 的 分页插件
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");
        PageHelper.startPage(page,limit);

        List<OutStorage> outStorages = outStorageMapper.selectByParams(params);
        // mybatis 的 分页数据结构
        PageInfo<OutStorage> outStoragePageInfo = new PageInfo<>(outStorages);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<OutStorage> studentLayuiPageVo = new LayuiPageVo<>();
        studentLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        studentLayuiPageVo.setMsg("分页列表数据"); // 提示信息
        studentLayuiPageVo.setCount(outStoragePageInfo.getTotal()); // 总记录数
        studentLayuiPageVo.setData(outStoragePageInfo.getList());  // 分页的列表数据
        return studentLayuiPageVo;
    }

//    @Override
//    public int outStorageAdd(OutStorage outStorage) {
//        return outStorageMapper.outStorageAdd(outStorage);
//    }

    @Override
    public int outStorageAdd(OutStorage outStorage) {
        //获得入库的数量
        Integer number = outStorage.getNumber();
        //查询现在仓库中已经有的数量
        Integer i = outStorageMapper.storageSelect(outStorage.getName());
        //更新仓库的数量
        String stock = String.valueOf(i-number);
//        Storage storage = new Storage();
//        //outStorage把的名字给这个新的storage
//        storage.setName(outStorage.getName());
//        //设置库存为更新后的库存
//        storage.setStock(String.valueOf(stock));
        //更新仓库
         outStorageMapper.storageUpdate(outStorage.getName(),stock);

        //添加语句
        return outStorageMapper.outStorageAdd(outStorage);
    }


    @Override
    public int outStorageUpdate(OutStorage outStorage) {
        return outStorageMapper.outStorageUpdate(outStorage);
    }

    @Override
    public int outStorageDelete(int id) {
        return outStorageMapper.outStorageDelete(id);
    }

    @Override
    public List<OutStorageBo> outStorageExcelData(OutStorage outStorage) {
        List<OutStorage> outStorages = outStorageMapper.selectByKey(outStorage);
        List<OutStorageBo> collect = outStorages.stream().map((o) -> {
            OutStorageBo outStorageBo = new OutStorageBo();
            outStorageBo.setId(o.getId());
            outStorageBo.setName(o.getName());
            outStorageBo.setType(o.getType());
            outStorageBo.setAddress(o.getAddress());
            outStorageBo.setNumber(o.getNumber());
            outStorageBo.setManger(o.getManger());
            outStorageBo.setPhone(o.getPhone());
            outStorageBo.setDate(o.getDate());
            return outStorageBo;
        }).collect(Collectors.toList());

        return collect;
    }



    @Override
    public int deleteStoragesByIds(String ids) {
        String[] arrId = ids.split(",");
        return outStorageMapper.deleteMessageByIds(arrId);

    }


    @Override
    public List<OutStorageVo> datePieData() {

        List<OutStorageVo> list = outStorageMapper.datePieData();
        return list;
    }


}
